import MusicManager from "../../MusicManager";
import Utils from "../../Utils";
import NoviceGuide from "../UI/NoviceGuide";
import { FacilityType, GamePropType } from "../info/GameEnum";
import { Global } from "../info/Global";
import { Location } from "../info/Predefine";
import GameManager from "../manager/GameManager";
import PlayerManager from "../manager/PlayerManager";
import UiManager from "../manager/UiManager";
import ControlledPlayer from "../player/ControlledPlayer";
import Player from "../player/Player";
import Dialog from "./Dialog";

const { ccclass, property } = cc._decorator;

/**
 * 建造弹窗
 */
@ccclass
export default class DomainDialog extends Dialog {

    public static Locked = false;

    @property(cc.Button)
    魔盒: cc.Button = null;

    @property(cc.Button)
    近防炮: cc.Button = null;

    @property(cc.Button)
    侦察机基地: cc.Button = null;

    @property(cc.Button)
    领土扩张器: cc.Button = null;

    @property(cc.Button)
    矿机: cc.Button = null;

    @property(cc.Button)
    榴弹炮基地: cc.Button = null;

    @property(cc.Button)
    火箭炮基地: cc.Button = null;

    @property(cc.Button)
    电磁炮基地: cc.Button = null;

    @property(cc.Button)
    照明弹基地: cc.Button = null;

    @property(cc.Button)
    洲际导弹基地: cc.Button = null;

    @property(cc.Button)
    核弹基地: cc.Button = null;

    @property(cc.Button)
    卫星: cc.Button = null;

    location: Location;

    controllerPlayer: ControlledPlayer;

    protected onEnable(): void {
        this.controllerPlayer = PlayerManager.instance.controllerPlayer;
        this.controllerPlayer.node.on(Player.EventType.更新晶石, this.onAssetsChange, this);
        this.controllerPlayer.node.on(Player.EventType.更新石油, this.onAssetsChange, this);
    }

    protected onDisable(): void {
        cc.isValid(this.controllerPlayer) && this.controllerPlayer.node.targetOff(this);
    }

    public init(location: Location) {
        this.location = location;
        this.resetMagicBoxBtn();
        this.resetBtn(this.近防炮, FacilityType.近防炮);
        this.resetBtn(this.侦察机基地, FacilityType.侦察机基地);
        this.resetBtn(this.领土扩张器, FacilityType.领土扩张器);
        this.resetBtn(this.矿机, FacilityType.矿机);
        this.resetBtn(this.榴弹炮基地, FacilityType.榴弹炮基地);
        this.resetBtn(this.火箭炮基地, FacilityType.火箭炮基地);
        this.resetBtn(this.电磁炮基地, FacilityType.电磁炮基地);
        this.resetBtn(this.照明弹基地, FacilityType.照明弹基地);
        this.resetBtn(this.洲际导弹基地, FacilityType.洲际导弹基地);
        this.resetBtn(this.核弹基地, FacilityType.核弹基地);
        this.resetBtn(this.卫星, FacilityType.卫星);
    }

    private onAssetsChange() {
        this.init(this.location);
    }

    private resetMagicBoxBtn() {
        let magicBoxTime = PlayerManager.instance.controllerPlayer.magicBoxTime;
        let prices = Global.魔盒价格;
        let currPrice = prices[magicBoxTime];
        let surplus = Math.min(prices.length - magicBoxTime, Global.gameInfo.propCount[GamePropType.魔盒]);
        let limitTip = this.魔盒.node.getChildByName("已达上限");
        let priceNode = this.魔盒.node.getChildByName("price");
        let diamondNode = priceNode.getChildByName("diamond");
        let oilNode = priceNode.getChildByName("oil");

        cc.find("sumtip/count", this.魔盒.node).getComponent(cc.Label).string = surplus.toString();
        if (surplus == 0) {
            this.魔盒.interactable = false;
            limitTip.active = true;
            priceNode.active = false;
            if (Global.gameInfo.propCount[GamePropType.魔盒] == 0) {
                limitTip.getComponent(cc.Label).string = "道具不足";
            } else {
                limitTip.getComponent(cc.Label).string = "已达上限";
            }
            return;
        } else {
            this.魔盒.interactable = true;
            limitTip.active = false;
        }


        diamondNode.active = currPrice.diamond > 0;
        oilNode.active = currPrice.oil > 0;
        diamondNode.getComponentInChildren(cc.Label).string = currPrice.diamond.toString();
        oilNode.getComponentInChildren(cc.Label).string = currPrice.oil.toString();
        priceNode.active = !limitTip.active
        this.魔盒.interactable = priceNode.active && PlayerManager.instance.controllerPlayer.checkEnoughMoney(currPrice);
    }

    private resetBtn(btn: cc.Button, facilityType: FacilityType) {
        let result = PlayerManager.instance.controllerPlayer.checkFacilityCondition(facilityType);
        btn.node.active = result[2];
        if (!btn.node.active) return;
        let price = PlayerManager.instance.controllerPlayer.getPrice(facilityType);
        let priceNode = btn.node.getChildByName("price");
        let diamondNode = priceNode.getChildByName("diamond");
        let oilNode = priceNode.getChildByName("oil");
        let limitTip = btn.node.getChildByName("已达上限");

        diamondNode.active = price.diamond > 0;
        oilNode.active = price.oil > 0;
        diamondNode.getComponentInChildren(cc.Label).string = price.diamond.toString();
        oilNode.getComponentInChildren(cc.Label).string = price.oil.toString();
        limitTip && (limitTip.active = !result[3], priceNode.active = !limitTip.active);
        btn.interactable = priceNode.active && result[0] && result[1];
    }

    private getBuildSite(size: number): Location[] {
        let player = PlayerManager.instance.controllerPlayer;
        let sites = [];
        if (size == 4) {
            if (player.canBeUesd({ locationX: this.location.locationX, locationY: this.location.locationY - 1 })) {
                if (player.canBeUesd({ locationX: this.location.locationX - 1, locationY: this.location.locationY }) && player.canBeUesd({ locationX: this.location.locationX - 1, locationY: this.location.locationY - 1 })) {
                    sites.push([this.location, { locationX: this.location.locationX - 1, locationY: this.location.locationY }, { locationX: this.location.locationX - 1, locationY: this.location.locationY - 1 }, { locationX: this.location.locationX, locationY: this.location.locationY - 1 }]);
                }
                if (player.canBeUesd({ locationX: this.location.locationX + 1, locationY: this.location.locationY }) && player.canBeUesd({ locationX: this.location.locationX + 1, locationY: this.location.locationY - 1 })) {
                    sites.push([this.location, { locationX: this.location.locationX + 1, locationY: this.location.locationY }, { locationX: this.location.locationX + 1, locationY: this.location.locationY - 1 }, { locationX: this.location.locationX, locationY: this.location.locationY - 1 }]);
                }
            }
            if (player.canBeUesd({ locationX: this.location.locationX, locationY: this.location.locationY + 1 })) {
                if (player.canBeUesd({ locationX: this.location.locationX - 1, locationY: this.location.locationY }) && player.canBeUesd({ locationX: this.location.locationX - 1, locationY: this.location.locationY + 1 })) {
                    sites.push([this.location, { locationX: this.location.locationX - 1, locationY: this.location.locationY }, { locationX: this.location.locationX - 1, locationY: this.location.locationY + 1 }, { locationX: this.location.locationX, locationY: this.location.locationY + 1 }]);
                }
                if (player.canBeUesd({ locationX: this.location.locationX + 1, locationY: this.location.locationY }) && player.canBeUesd({ locationX: this.location.locationX + 1, locationY: this.location.locationY + 1 })) {
                    sites.push([this.location, { locationX: this.location.locationX + 1, locationY: this.location.locationY }, { locationX: this.location.locationX + 1, locationY: this.location.locationY + 1 }, { locationX: this.location.locationX, locationY: this.location.locationY + 1 }]);
                }
            }
        } else if (size == 3) {
            if (player.canBeUesd({ locationX: this.location.locationX, locationY: this.location.locationY - 1 }) && player.canBeUesd({ locationX: this.location.locationX, locationY: this.location.locationY - 2 })) {
                sites.push([this.location, { locationX: this.location.locationX, locationY: this.location.locationY - 1 }, { locationX: this.location.locationX, locationY: this.location.locationY - 2 }])
            }
            if (player.canBeUesd({ locationX: this.location.locationX, locationY: this.location.locationY - 1 }) && player.canBeUesd({ locationX: this.location.locationX, locationY: this.location.locationY + 1 })) {
                sites.push([this.location, { locationX: this.location.locationX, locationY: this.location.locationY - 1 }, { locationX: this.location.locationX, locationY: this.location.locationY + 1 }])
            }
        } else if (size == 2) {
            if (player.canBeUesd({ locationX: this.location.locationX, locationY: this.location.locationY - 1 })) {
                sites.push([this.location, { locationX: this.location.locationX, locationY: this.location.locationY - 1 }]);
            }
            if (player.canBeUesd({ locationX: this.location.locationX, locationY: this.location.locationY + 1 })) {
                sites.push([this.location, { locationX: this.location.locationX, locationY: this.location.locationY + 1 }]);
            }
        } else if (size == 1) {
            sites.push([this.location]);
        }
        return Utils.getRandomChoose(sites);
    }

    bulidMagicBoxFacility() {
        let magicBoxTime = PlayerManager.instance.controllerPlayer.magicBoxTime;
        let price = Global.魔盒价格[magicBoxTime];
        PlayerManager.instance.controllerPlayer.magicBoxTime++;
        Global.gameInfo.propCount[GamePropType.魔盒]--;
        Global.saveGameInfo();
        let magicBoxFacility = [
            { type: FacilityType.风电站, site: 1 },
            { type: FacilityType.火电站, site: 1 },
            { type: FacilityType.光伏板, site: 1 },
            { type: FacilityType.反导雷达, site: 1 },
            { type: FacilityType.黑洞掠夺者, site: 4 },
            { type: FacilityType.蜂刺反导导弹基地, site: 4 },
            { type: FacilityType.激光器, site: 1 },
            { type: FacilityType.超级激光炮, site: 2 },
            { type: FacilityType.黑洞发生器, site: 4 }
        ];
        if (!this.getBuildSite(4)) {
            if (this.getBuildSite(2)) {
                magicBoxFacility = magicBoxFacility.filter(item => item.site <= 2);
            } else {
                magicBoxFacility = magicBoxFacility.filter(item => item.site == 1);
            }
        }

        let threadArr = magicBoxFacility.map(item => Math.max(1, 1000 - Global.getFacilityInfo<{ 威胁值: number }>(item.type).威胁值))
        let choose = Utils.getRandomIndexByProbability(threadArr);

        let locaitons = this.getBuildSite(magicBoxFacility[choose].site);
        let finalPosition = cc.v3(0, 0);
        for (let i = 0; i < locaitons.length; i++) {
            finalPosition.addSelf(GameManager.instance.getGamePosition(locaitons[i]));
        }
        finalPosition.divSelf(locaitons.length);

        DomainDialog.Locked = true;
        GameManager.instance.addMagicBoxEffect(finalPosition, () => {
            DomainDialog.Locked = false;
            PlayerManager.instance.controllerPlayer.addFacility(magicBoxFacility[choose].type, locaitons, price);
        })
    }

    onClick(event: cc.Event) {
        let player = PlayerManager.instance.controllerPlayer
        let name = event.target.name;
        MusicManager.instance.play(MusicManager.instance.btnMove);
        let locations;
        switch (name) {
            case "魔盒":
                this.bulidMagicBoxFacility();
                break;
            case "近防炮":
                player.addFacility(FacilityType.近防炮, [this.location]);
                player.node.emit(NoviceGuide.EventType.建造设施, FacilityType.近防炮);
                break;
            case "侦察机":
                player.addFacility(FacilityType.侦察机基地, [this.location]);
                player.node.emit(NoviceGuide.EventType.建造设施, FacilityType.侦察机基地);
                break;
            case "领土扩张器":
                player.addFacility(FacilityType.领土扩张器, [this.location]);
                break;
            case "矿机":
                player.addFacility(FacilityType.矿机, [this.location]);
                player.node.emit(NoviceGuide.EventType.建造设施, FacilityType.矿机);
                break;
            case "榴弹炮":
                player.addFacility(FacilityType.榴弹炮基地, [this.location]);
                break;
            case "照明弹":
                player.addFacility(FacilityType.照明弹基地, [this.location]);
                break;
            case "火箭炮":
                locations = this.getBuildSite(4);
                if (locations) {
                    player.addFacility(FacilityType.火箭炮基地, locations);
                } else {
                    UiManager.instance.showTip("当前位置不可建造!");
                }
                break;
            case "电磁炮":
                locations = this.getBuildSite(4);
                if (locations) {
                    player.addFacility(FacilityType.电磁炮基地, locations);
                } else {
                    UiManager.instance.showTip("当前位置不可建造!");
                }
                break;
            case "洲际导弹":
                locations = this.getBuildSite(3);
                if (locations) {
                    player.addFacility(FacilityType.洲际导弹基地, locations);
                } else {
                    UiManager.instance.showTip("当前位置不可建造!");
                }
                break;
            case "核弹":
                locations = this.getBuildSite(4);
                if (locations) {
                    player.addFacility(FacilityType.核弹基地, locations);
                } else {
                    UiManager.instance.showTip("当前位置不可建造!");
                }
                break;
            case "卫星":
                locations = this.getBuildSite(4);
                if (locations) {
                    player.addFacility(FacilityType.卫星, locations);
                } else {
                    UiManager.instance.showTip("当前位置不可建造!");
                }
                break;
        }
        this.closeDialog();
    }

    public closeDialog(): void {
        this.node.active = false;
    }
}
